<template>
  <div class="home">
    
    <HelloWorld msg="Welcome to Your Vue.js App"/>
  </div>
</template>

<script>
// @ is an alias to /src
import HelloWorld from '@/components/HelloWorld.vue'
import axios from 'axios'
// 项目的appId
var appid = 'wx7640ff0e16925d0d';
// 授权后 需要跳回的页面 一般是当前页面
var redirectUrl = 'http://bufantec.natapp1.cc/home';
// 引导用户获取code的api
var code_url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${redirectUrl}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect`;
// 接口服务器地址: 
const serveHost = 'http://www.bufantec.com';
export default {
  name: 'Home',
  components: {
    HelloWorld
  },
  data(){
    return {
      code: ''
    }
  },
  created(){
    // 在首页 实现默认获取openId
    // 1.引导用户获取code
    this.code = this.$route.query.code;
    console.log('this.code',this.code)
    if(this.code){
      // 存在code  拿code去后台交换openId
    //   + 获取openId的api  /wx/user/getOpenId
    // - 参数 code
    // - 参数 appid
      var url = `${serveHost}/wx/user/getOpenId?appid=${appid}&code=${this.code}`
      axios.get(url)
        .then(res=>{
          console.log(res);
          window.localStorage.setItem('openid',res.data.data.openid);
          alert('登陆成功!');
        })
    }else{
      window.location.href = code_url;
    }
  },
  methods(){

  }
}
</script>
